<?php

// +----------------------------------------------------------------------
// | 第三方支付
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2022 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\event\pay;


use app\common\event\Base;
use app\event\ucenter\User as UserEvent;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use think\facade\Db;
use app\model\ucenter\User as UserModel;
use app\model\pay\Pay as PayModel;
use app\model\ucenter\UserCash as UserCashModel;
use think\Exception;

class Bill extends Base
{


    /**
     * 代理月账单
     */
    public static function agentMonth()
    {
        try {
            $users = UserModel::where('is_agency', 1)->select();
            foreach ($users as $index => $user) {
                $bill = $user->bills()->whereTime('bill_time', 'month')->lock(true)->find();
                if (!$bill) {
                    $bill['sn'] = time_format('last month', 'Ym');
                    $bill['name'] = '服务商' . date('m', strtotime('last month')) . '月结账单';
                    $bill['channel'] = '';
                    $bill['user'] = UserModel::where('agent_id', $user['id'])->whereTime('effective_time', 'last month')->count();
                    // 结算等级
                    if ($bill['user'] <= 30) {
                        $bill['rebate_level'] = 1;
                    } elseif ($bill['user'] <= 60) {
                        $bill['rebate_level'] = 2;
                    } else {
                        $bill['rebate_level'] = 3;
                    }

                    // 保证金
                    $bill['deposit_amount'] = PayModel::where('agent_id', $user['id'])->where('type', 'deposit')->whereTime('pay_time', 'last month')->sum('total_amount');
                    // 提现笔数
                    $bill['cash_amount'] = UserCashModel::where('agent_id', $user['id'])
                        ->where('user_id', '<>', $user['id'])
                        ->where('cash_charge', '>', 2)
                        ->whereTime('create_time', 'last month')
                        ->where('status', 1)->count();
                    // 展业收益
                    $bill['profit_amount'] = UserMoneyLogModel::where('agent_id', $user['id'])
                        ->where('user_id', '<>', $user['id'])->whereTime('create_time', 'last month')
//                        ->where('type', 'in', [11, 90])
                        ->where('is_work', '<>', 0)
                        ->sum('money');
                    if ($bill['rebate_level'] == 1) {
                        $bill['amount'] = ($bill['cash_amount'] * 2 + $bill['deposit_amount']) * 0.2 + $bill['profit_amount'] * 0.03;
                    } elseif ($bill['rebate_level'] == 2) {
                        $bill['amount'] = ($bill['cash_amount'] * 2 + $bill['deposit_amount']) * 0.3 + $bill['profit_amount'] * 0.04;
                    } else {
                        $bill['amount'] = ($bill['cash_amount'] * 2 + $bill['deposit_amount']) * 0.4 + $bill['profit_amount'] * 0.05;
                    }
                    $bill['trade'] = 0;
                    $bill['bill_time'] = time_format();
                    $re = $user->bills()->save($bill);
                    if ($re && $bill['amount'] >= 0.01) {
                        UserEvent::money($bill['amount'], $user['id'], $bill['name'], 66, $user['id'], $bill['sn']);
                    }
                    //邀请人
                    if ($user['agency_time'] > '2025-02-11 00:00:00' && $user['pid'] && $bill['profit_amount'] > 0 && $bill['profit_amount'] * 0.002 > 0.01) {
                        $pUser = UserModel::where('id', $user['pid'])->find();
                        if ($pUser) {
                            UserEvent::money($bill['profit_amount'] * 0.002, $pUser['id'], '邀请服务商享受分佣提成2‰', 66, $user['id'], $bill['sn']);
                        }
                    }
                }
            }

            $data['message'] = 'ok';
            $data['code'] = 0;
        } catch (Exception $e) {
            //                 回滚事务
            Db::rollback();
            $data['message'] = $e->getMessage();
            $data['code'] = 1;
        }
        return $data;
    }

}
